package org.zhq.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zhq.Const;
import org.zhq.mapper.CategoryMapper;
import org.zhq.pojo.Category;
import org.zhq.pojo.vo.CategoryVO;
import org.zhq.pojo.vo.NewItemsVO;
import org.zhq.service.CategoryService;

import java.util.HashMap;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    private final CategoryMapper categoryMapper;
    private final StringRedisTemplate redisTemplate;

    @Autowired
    public CategoryServiceImpl(CategoryMapper categoryMapper, StringRedisTemplate redisTemplate) {
        this.categoryMapper = categoryMapper;
        this.redisTemplate = redisTemplate;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Category> queryAllRootLevelCat() {
        String catsStr = redisTemplate.opsForValue().get(Const.Redis.CATS);
        List<Category> list;
        if (StrUtil.isBlank(catsStr)) {
            LambdaQueryWrapper<Category> condition = Wrappers.<Category>lambdaQuery().eq(Category::getType, 1);
            list = categoryMapper.selectList(condition);
            redisTemplate.opsForValue().set(Const.Redis.CATS, JSONUtil.toJsonStr(list));
        } else {
            list = JSONUtil.toList(JSONUtil.parseArray(catsStr), Category.class);
        }
        return list;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<CategoryVO> getSubCatList(Integer rootCatId) {
        String subCatStr = redisTemplate.opsForValue().get(Const.Redis.SUB_CAT + ":" + rootCatId);
        List<CategoryVO> list;
        if (StrUtil.isBlank(subCatStr)) {
            list = categoryMapper.getSubCatList(rootCatId);
            redisTemplate.opsForValue().set(Const.Redis.SUB_CAT + ":" + rootCatId, JSONUtil.toJsonStr(list));
        } else {
            list = JSONUtil.toList(JSONUtil.parseArray(subCatStr), CategoryVO.class);
        }
        return list;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<NewItemsVO> getSixNewItemsLazy(Integer rootCatId) {
        String sixNewItemsStr = redisTemplate.opsForValue().get(Const.Redis.SIX_NEW_ITEMS + ":" + rootCatId);
        List<NewItemsVO> list;
        if (StrUtil.isBlank(sixNewItemsStr)) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("rootCatId", rootCatId);
            list = categoryMapper.getSixNewItemsLazy(map);
            redisTemplate.opsForValue().set(Const.Redis.SIX_NEW_ITEMS + ":" + rootCatId, JSONUtil.toJsonStr(list));
        } else {
            list = JSONUtil.toList(JSONUtil.parseArray(sixNewItemsStr), NewItemsVO.class);
        }
        return list;
    }
}
